#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

class Solution {
public:
    int romanToInt(string s) {
        map<char,int> M = {make_pair('I',1),make_pair('V',5),make_pair('X',10),make_pair('L',50),
        make_pair('C',100),make_pair('D',500),make_pair('M',1000)};
        int result=0;
        for(int i=s.size()-1;i>=0;--i){
            if(i!=0){
                if(M[s[i]]>M[s[i-1]]){
                    result = result+(M[s[i]]-M[s[i-1]]);
                    i = i-1;
                }
                else{
                    result = result+M[s[i]];
                }
            
            }
            else{
                result = result+M[s[i]];
            }  
        }
        return result;
        
    }
};


int main(){
    Solution S;
    cout<<S.romanToInt("LVIII")<<endl;
    system("pause");

}